package com.example.myapplication;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.myapplication.model.Fruit;

import java.util.ArrayList;
import java.util.List;

public class MyAdapter extends BaseAdapter {

    //声明一个List变量，作为数据容器
    private List<Fruit> list = new ArrayList<>();

    //用来保存ListView所在的 Activity 上下文环境，可以用它来展开布局视图
    private Activity mActivity;

    //构造方法
    public MyAdapter(List<Fruit> data, Activity a) {
        //在构造的时候将数据容器填满
        list = data;
        //保存Activity实例
        mActivity = a;
    }

    //当ListView需要时,返回容器中的数据数量
    @Override
    public int getCount() {
        return list.size();
    }

    //当ListView请求第i个数据时,返回容器中第i个数据
    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    //当ListView请求第i个数据的Id时候，直接返回i作为Id
    @Override
    public long getItemId(int i) {
        return i;
    }

    //当ListView请求第i个数据的视图对象时，展开布局文件，设置对应的水果，并将视图交给 ListView
    @Override
    public View getView(int i, View convertView, ViewGroup parent) {

        ViewHolder viewHolder;

        if( convertView == null) {
            viewHolder = new ViewHolder();
            //展开 item.xml 视图
            convertView = LayoutInflater.from(mActivity).inflate(R.layout.item, null);
            //找到图片视图对象
            viewHolder.imgView = convertView.findViewById(R.id.img);
            //找到文字视图对象
            viewHolder.nameTv = convertView.findViewById(R.id.name);
            viewHolder.btn = convertView.findViewById(R.id.btn);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        //找到容器中第i个水果的图片的 Drawable 对象
        Drawable drawable = convertView.getResources().getDrawable(  list.get(i).imgId );
        //将Drawable 绘制到图片视图中
        viewHolder.imgView.setImageDrawable(drawable);

        //将容器中第i个水果的名字，设置到文字视图中
        viewHolder.nameTv.setText(list.get(i).name);
        final String nameStr = list.get(i).name;
        final String priceStr = list.get(i).price + "元";
        viewHolder.btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
                builder.setTitle("购买水果")
                        .setMessage("是否购买" + nameStr + "? 单价：" + priceStr)
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {

                            }
                        })
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {

                            }
                        });
                builder.create().show();
            }
        });
        //返回设置好的视图
        return convertView;
    }


    private class ViewHolder {
        TextView  nameTv;
        TextView  priceTv;
        ImageView imgView;
        Button  btn;
    }

}
